<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/4 0004
 * Time: 14:44
 */

namespace app\api\controller;


use app\admin\model\Form;
use app\admin\model\Member;
use app\common\BaseHelper;
use think\Db;

class Address extends Base
{
    /**
     * 保存地址
     */
    public function saveAddress()
    {
        $param = self::getHttpParam();
        if (empty($param->memberId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'memberId不能为空');
        }
        if (empty($param->name)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '收货人姓名（name）不能为空');
        }
        if (empty($param->phone)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '收货人电话（phone）不能为空');
        }
        if (empty($param->province)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '省（province）不能为空');
        }
        if (empty($param->city)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '市（city）不能为空');
        }
        if (empty($param->area)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '区（area）不能为空');
        }
        if (empty($param->details)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '详细地址（details）不能为空');
        }
        if (!empty($param->formId)) {
            $formMdl = new Form();
            $member = Member::where(['id' => $param->memberId])->find();
            if (!empty($param->formId)) {
                $data = [
                    'id' => $param->formId,
                    'phone' => $member['account_number'],
                    'create_date' => date('Y-m-d H:i:s', time()),
                ];
                $formMdl->save($data);
            }
        }

        Db::startTrans();
        try {
            /*$temp = \app\admin\model\Address::where(['memberId' => $param->memberId, 'status' => 0])->find();
            if (!empty($temp)) {
                $temp['status'] = 1;
                $temp->save();
            }*/
            $model = new \app\admin\model\Address();
            $data = (array)$param;

            // 是否是默认地址
            if ($param->status == 0) {
                // 将默认地址改为普通地址
                $model->where(['memberId' => $param->memberId, 'status' => 0])->update(['status' => 1]);
            }

            if (!empty($param->id)) {
                $id = $param->id;
                $data['updateDate'] = time();
                $message = '保存失败';
                $result = $model->allowField(true)->save($data, ['id' => $id]);
            } else {
                $data['id'] = BaseHelper::getUUID();
                $data['updateDate'] = date('Y-m-d H:i:s', time());
                $message = '创建失败';
                $result = $model->save($data);
            }

            if ($result !== false) {
                Db::commit();
            } else {
                Db::rollback();
                return show(config('status.ERROR_STATUS'), $message, $model->getError());
            }
        } catch (Exception $e) {
            Db::rollback();
            return show(config('status.ERROR_STATUS'), '操作失败', $e->getMessage());
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, null);
    }

    /**
     * 删除地址
     */
    public function delAddress()
    {
        $param = self::getHttpParam();
        if (empty($param->id)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'id不能为空');
        }
        if (empty($param->memberId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'memberId不能为空');
        }

        $model = \app\admin\model\Address::where(['id' => $param->id])->find();
        if ($model['memberId'] == $param->memberId) {
            $model->delete();
        } else {
            return show(config('status.ERROR_STATUS'), '删除失败', '只能删除本人创建的地址');
        }
        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, null);
    }

    /**
     * 查询我的地址
     */
    public function getAddress()
    {
        $param = self::getHttpParam();
        if (empty($param->memberId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'memberId不能为空');
        }

        $list = \app\admin\model\Address::where(['memberId' => $param->memberId])->order('status ASC, create_date DESC')->select();
        if (!empty($list)) {
            return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list);
        } else {
            return show(config('status.SUCCESS_STATUS'), '没有收货地址', []);
        }
    }
}